package com.hello.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hello.common.log.annotation.Log;
import com.hello.common.log.enums.BusinessType;
import com.hello.system.domain.TreeHoleEntries;
import com.hello.system.domain.TreeHoleReports;
import com.hello.system.service.ITreeHoleEntriesService;
import com.hello.system.service.ITreeHoleReportsService;
import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.common.core.utils.poi.ExcelUtil;
import com.hello.common.core.web.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 树洞举报Controller
 * 
 * @author hello
 * @date 2025-05-28
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/reports")
public class TreeHoleReportsController extends BaseController
{
    private final ITreeHoleReportsService treeHoleReportsService;
    private final ITreeHoleEntriesService treeHoleEntriesService;

    /**
     * 查询树洞举报列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TreeHoleReports treeHoleReports)
    {
        startPage();
        List<TreeHoleReports> list = treeHoleReportsService.selectTreeHoleReportsList(treeHoleReports);
        return getDataTable(list);
    }

    /**
     * 导出树洞举报列表
     */
    @Log(title = "树洞举报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TreeHoleReports treeHoleReports)
    {
        List<TreeHoleReports> list = treeHoleReportsService.selectTreeHoleReportsList(treeHoleReports);
        ExcelUtil<TreeHoleReports> util = new ExcelUtil<TreeHoleReports>(TreeHoleReports.class);
        util.exportExcel(response, list, "树洞举报数据");
    }

    /**
     * 获取树洞举报详细信息
     */
    @GetMapping(value = "/{reportId}")
    public AjaxResult getInfo(@PathVariable("reportId") Long reportId)
    {
        return success(treeHoleReportsService.selectTreeHoleReportsByReportId(reportId));
    }

    /**
     * 新增树洞举报
     */
    @Log(title = "树洞举报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TreeHoleReports treeHoleReports)
    {
        try {
            // 检查必填参数
            if (treeHoleReports.getTreeHoleId() == null) {
                return error("树洞ID不能为空");
            }
            
            // 验证树洞是否存在
            TreeHoleEntries entry = treeHoleEntriesService.selectTreeHoleEntriesByEntryId(treeHoleReports.getTreeHoleId());
            if (entry == null) {
                return error("树洞内容不存在");
            }
            
            // 执行插入操作
            return toAjax(treeHoleReportsService.insertTreeHoleReports(treeHoleReports));
        } catch (Exception e) {
            log.error("新增树洞举报失败", e);
            return error("新增树洞举报失败：" + e.getMessage());
        }
    }

    /**
     * 修改树洞举报
     */
    @Log(title = "树洞举报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TreeHoleReports treeHoleReports)
    {
        try {
            // 检查必填参数
            if (treeHoleReports.getTreeHoleId() == null) {
                return error("树洞ID不能为空");
            }
            
            // 验证树洞是否存在
            TreeHoleEntries entry = treeHoleEntriesService.selectTreeHoleEntriesByEntryId(treeHoleReports.getTreeHoleId());
            if (entry == null) {
                return error("树洞内容不存在");
            }
            
            // 执行更新操作
            return toAjax(treeHoleReportsService.updateTreeHoleReports(treeHoleReports));
        } catch (Exception e) {
            log.error("修改树洞举报失败", e);
            return error("修改树洞举报失败：" + e.getMessage());
        }
    }

    /**
     * 删除树洞举报
     */
    @Log(title = "树洞举报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reportIds}")
    public AjaxResult remove(@PathVariable Long[] reportIds)
    {
        return toAjax(treeHoleReportsService.deleteTreeHoleReportsByReportIds(reportIds));
    }
}
